<?php
/*------------------------------------------------------------------------
 * CustomAgreement.class.php 
 *
 * 定制合同
 * 	
 * Created on 2015-12-17
 *
 * Author: hwc123 <hwc123@139.com>
 * 
 * Copyright (c) 2015 http://www.qingtajiao.com All rights reserved.
 * ------------------------------------------------------------------------
 */

namespace Home\Controller;
class CustomAgreementController extends CommonController{

	public function _initialize(){
		parent::_initialize();
		$this->add_step('律师服务', URL('index'));
	}
	// 合同主页
	// public function index(){

	// 	$list = D('CustomAgreement')->get_sort_agreement_list();
	// 	// 创业学院活动公告banner
	// 	$banner = D('Banner')->get_list('agreement');

	// 	$this->assign('sort_agreement_list', $list);
	// 	$this->assign('banner', $banner);
	// 	$this->display();
	// }
	// 2.1版本首页
	public function index(){
		// Banner
		$this->banner  = D('Banner')->get_list('agreement');
		// 获取显示类别
		$show_sort_id  = Conf('show_sort_id', 'custom_agreement');
		// 获取其它类别
		$other_sort_id = Conf('other_sort_id', 'custom_agreement');
		// 合同列表
		$this->list    = D('CustomAgreement')->get_list(array('category_id' => $show_sort_id));
		
		$this->display();
	}
	
	// 2.1版本详情
	public function detail(){
		$agreement_id = $this->_get('agreement_id', '对不起，您的操作有误！');
		$info = D('CustomAgreement')->info($agreement_id);
		if(!$info){
			$this->error('找不到相关数据！');
		}
		$this->add_step($info['title']);

		$saas_key = $this->_get('saas_key');
		if($saas_key){
			session('saas_key', $saas_key);
			session('is_saas_order', true);
		}
		// 更新查看人数
		D('CustomAgreement')->update_view_num($agreement_id);

		// 套餐清单
		$this->package_list = D('CustomAgreement')->get_list(array(
			'id' => array('in', $info['package_agreement_ids'])
		));
		// 套餐原价格
		$this->package_original_price = $info['service_price'];
		foreach ($this->package_list as $rs) {
			$this->package_original_price += $rs['service_price'];
		}
		$this->package_original_price = sprintf("%.2f" , $this->package_original_price);
		// 同类别合同清单
		$this->service_list = D('CustomAgreement')->get_list(array(
			'category_id' => $info['category_id']
		));
		// 显示类别的合同清单
		$this->show_list = D('CustomAgreement')->get_list(array(
			'category_id' => Conf('show_sort_id', 'custom_agreement')
		));
		// 其它类别的合同清单
		$this->other_list = D('CustomAgreement')->get_list(array(
			'category_id' => Conf('other_sort_id', 'custom_agreement')
		));

		$this->assign('info', $info);
		$this->display();
	}
	
	
	
	
	// 搜索
	public function search(){
		$keyword = $this->_post('keyword', '请输入关键字！');
		$where['title'] = array('like', '%' . $keyword . '%');
		$info = D('CustomAgreement')->where($where)->find();
		if(!$info){
			$this->error('对不起，您搜索内容不存在！');
		}
		redirect(U('detail', array('agreement_id' => $info['id'])));
	}

	// 合同详情
	// public function detail(){
	// 	$agreement_id = $this->_get('agreement_id', '对不起，您的操作有误！');
	// 	$info = D('CustomAgreement')->info($agreement_id);
	// 	if(!$info){
	// 		$this->error('找不到相关数据！');
	// 	}
	// 	$this->add_step($info['category_name']);
	// 	$this->add_step($info['title']);
	// 	$saas_key = $this->_get('saas_key');
	// 	if($saas_key){
	// 		session('saas_key', $saas_key);
	// 		session('is_saas_order', true);
	// 	}
	// 	// 更新查看人数
	// 	D('CustomAgreement')->update_view_num($agreement_id);

	// 	$where['agreement_id'] = $agreement_id;
	// 	$list = D('Comment')->get_list($where, '', $this->_get('p', '', 1), $this->pagesize);
	// 	//处理AJAX分页
	// 	$this->page($list, D('Comment')->count);
	// 	// 评价标签
	// 	$comment_label = D('Comment')->get_comment_label($agreement_id);

	// 	$this->assign('list', $list);
	// 	$this->assign('info', $info);
	// 	$this->assign('comment_label', $comment_label);
	// 	$this->display();
	// }
	// 提交订单
	public function submit_order(){
		$agreement_id = $this->_get('agreement_id', '对不起，您的操作有误！');
		$is_package   = $this->_get('is_package', '', 0);
		if(IS_POST){
			$user_id  = $this->user_id(false);
			$demand   = $this->_post('demand');
			$realname = $this->_post('realname', '请填写联系人姓名！');
			$mobile   = $this->_post('mobile', '请填写联系人手机号！');
			$email    = $this->_post('email', '请填写验证码！');
			if($this->is_lawyer){
				$this->error('对不起，您为律师用户，无法下单！');
			}
			// 短信验证码处理
			if(!$user_id || $mobile != $this->user_mobile){
				$smscode = $this->_post('smscode', '请填写验证码！');
				if(D('Sms')->check_smscode($mobile, $smscode)){
					 $this->error('短信验证码不正确！');
				}
			}
			// 已登录，手机号与填写不一致处理
			if($mobile != $this->user_mobile && $user_id){
				D('User')->change_mobile($user_id, $mobile);
			}
			// 已登录，E-mail与填写不一致处理
			if($email != $this->user_email && $user_id){
				if(!D('User')->change_email($user_id, $email)){
					$this->error(D('User')->tips_info);
				}
			}
			// 已登录，姓名与填写不一致处理
			if($realname != $this->user_realname && $user_id){
				D('User')->edit_info($user_id, $realname);
			}
			// 未登录处理
			if(!$user_id){
				$user_id = D('User')->get_mobile_user_id($mobile);
				// 注册用户
				if(!$user_id){
					//E-mail判断
					if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email)) {
						$this->error('无效的 email 格式！');
					}
					if(D('User')->email_exists($email)){
						$this->error('您输入的E-mail已经注册过，无法使用！');
					}
					$user_info = D('User')->register($mobile, $email, '');
					$user_id   = $user_info['user_id'];
					if($user_info){
						$user_info = D('User')->edit_info($user_id, $realname);
						D('User')->save_session($user_info);
					}else{
						$this->error(D('User')->tips_info);
					}
				}else{
					// 自动登录
					$user_info = D('User')->edit_info($user_id, $realname, '', '', '', '', $email);
					$user_info['token'] = D('User')->make_token($user_id);
					D('User')->save_session($user_info);
				}
			}
			// 创建订单
			$order_sn = D('Order')->submit($user_id, $agreement_id, $demand, $is_package);
			if(!$order_sn){
				$this->error(D('Order')->tips_info);
			}
			// SaaS合同跳转
			if(session('is_saas_order')){
				session('is_saas_order', false);
				$this->success('下单成功！', Conf('saas_home_url') . '/agreement_custom/order.html');
			}
			// $this->success('下单成功！', U('Order/pay', array('order_sn' => $order_sn)));
			$this->success('下单成功！', U('UserOrder/detail', array('order_sn' => $order_sn)));
		}
		$info = D('CustomAgreement')->info($agreement_id);
		if(!$info){
			$this->error('找不到相关数据！');
		}
		$this->add_step($info['title']);

		if($is_package){
			$info['service_price'] = $info['package_price'];
			// 套餐清单
			$this->package_list = D('CustomAgreement')->get_list(array(
				'id' => array('in', $info['package_agreement_ids'])
			));
		}
		$this->assign('info', $info);
		$this->display();
	}
}